package com.nciae.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nciae.po.Dormitory;
import com.nciae.dao.DormitoryMapper;
import com.nciae.po.Rectification;
import com.nciae.po.Score;
import com.nciae.po.SecurityQuestion;
import com.nciae.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Guoliyu
 * @since 2022-03-30
 */
@Service
public class DormitoryServiceImpl extends ServiceImpl<DormitoryMapper, Dormitory> implements IDormitoryService {

    @Autowired
    private ISecurityQuestionService securityQuestionService;

    @Autowired
    private IRectificationService rectificationService;

    @Autowired
    private IScoreService scoreService;

    @Autowired
    private IDormitorySecurityQuestionService dormitorySecurityQuestionService;





    @Override
    public List<Dormitory> findAll() {
        return baseMapper.selectList(null);
    }

    @Override
    public IPage<Dormitory> findAll(int index, int capacity, Integer buildingNo, Integer roomNo) {
        Page page = new Page();
        page.setCurrent(index);
        page.setSize(capacity);
        QueryWrapper wrapper = new QueryWrapper();
        if (!Objects.isNull(buildingNo)) {
            wrapper.eq("building_no", buildingNo);
        }
        if (!Objects.isNull(roomNo)) {
            wrapper.eq("room_no", roomNo);
        }
        return baseMapper.selectPage(page, wrapper);
    }

    @Override
    public void add(Dormitory dormitory) {
        baseMapper.insert(dormitory);
    }

    @Override
    public void update(Dormitory dormitory) {
        baseMapper.updateById(dormitory);
    }

    @Override
    public void delete(Long id) {
        baseMapper.deleteById(id);
    }

    @Override
    public Dormitory findDormitoryById(Long id) {
       return baseMapper.selectById(id);
    }

    @Override
    public void addList(List<Dormitory> dormitories) {
        if (!Objects.isNull(dormitories)) {
            for(Dormitory dormitory : dormitories) {
                add(dormitory);
            }
        }
    }

    @Override
    public List<SecurityQuestion> findSecurityByDormitoryId(Long id) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("dormitory_id", id);
        List<SecurityQuestion> securityQuestions = securityQuestionService.selectList(wrapper);
        for(SecurityQuestion securityQuestion : securityQuestions) {
            Rectification rectification = rectificationService.findBySecurityQuestionId(securityQuestion.getId());
            if (!Objects.isNull(rectification)) {
                securityQuestion.setRectification(rectification.getContent());
            }
        }
        return securityQuestions;
    }

    @Override
    public List<Score> findScoreById(Long id) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("dormitory_id", id);
        List<Score> scores = scoreService.selectList(wrapper);
        return scores;
    }

    @Override
    public List<Dormitory> findDormitoryByHeadId(Long id) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("dermitory_head_id", id);
        return baseMapper.selectList(wrapper);
    }
}
